<template>
    <view class="container">
		
		
        <button class="button" type="primary" @click="calculatorDialogToggle">
            <text class="button-text">打开计算器</text>
        </button>
		
        <uni-popup ref="calculatorDialog" type="dialog">
            <uni-popup-dialog ref="calculatorClose" mode="input" title="计算器" :content="calculatorContent">
                <view class="calculator">
					
                    <view class="display">{{ displayValue }}</view>
					
                    <view class="buttons">
                        <button v-for="number in numbers" :key="number" @click="appendNumber(number)">{{ number }}</button>
                        <button v-for="operator in operators" :key="operator" @click="appendOperator(operator)">{{ operator }}</button>
                        <button @click="calculate">=</button>
                        <button @click="clear">C</button>
                    </view>
                </view>
            </uni-popup-dialog>
        </uni-popup>
		
		
		
		
    </view>
</template>

<script setup>
import { ref } from 'vue';


const calculatorDialog = ref(null);
const displayValue = ref('0');
const calculatorContent = ref('');


const calculatorDialogToggle = () => {
    if (calculatorDialog.value) {
        calculatorDialog.value.open();
    }
};


const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const operators = ['+', '-', '*', '/'];


const appendNumber = (number) => {
    if (displayValue.value === '0') {
        displayValue.value = number;
    } else {
        displayValue.value += number;
    }
};


const appendOperator = (operator) => {
    displayValue.value += operator;
};


const clear = () => {
    displayValue.value = '0';
};


const calculate = () => {
    try {
        const result = eval(displayValue.value);
        displayValue.value = result.toString();
    } catch (error) {
        uni.showToast({
            title: '计算错误，请检查输入',
            icon: 'none'
        });
    }
};
</script>


<style scoped>
.calculator {
    display: flex;
    flex-direction: column;
    align-items: center;
}
.display {
    width: 200px;
    height: 50px;
    border: 1px solid #ccc;
    margin-bottom: 10px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding: 0 10px;
    font-size: 24px;
}
.buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}
.button {
    padding: 10px;
    font-size: 18px;
}
</style>